package com.liangxy.news.util;

import android.util.Log;

import com.liangxy.news.api.NewsDetailResponse;
import com.liangxy.news.api.NewsResponse;
import com.liangxy.news.model.News;
import com.liangxy.news.model.NewsDetailInfo;
import com.liangxy.news.model.NewsDetailResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 提供模拟数据用于测试
 */
public class MockDataProvider {
    
    private static final String TAG = "MockDataProvider";
    private static final Map<String, List<News>> CATEGORY_NEWS_MAP = new HashMap<>();
    private static final Map<String, NewsDetailResult> NEWS_DETAILS_MAP = new HashMap<>();
    
    static {
        initMockData();
    }
    
    /**
     * 初始化模拟数据
     */
    private static void initMockData() {
        Log.d(TAG, "初始化模拟数据");
        // 创建各分类的模拟新闻数据
        createMockNewsForCategory("top", "头条");
        createMockNewsForCategory("guonei", "国内");
        createMockNewsForCategory("guoji", "国际");
        createMockNewsForCategory("yule", "娱乐");
        createMockNewsForCategory("tiyu", "体育");
        createMockNewsForCategory("junshi", "军事");
        createMockNewsForCategory("keji", "科技");
        createMockNewsForCategory("caijing", "财经");
        createMockNewsForCategory("shishang", "时尚");
        Log.d(TAG, "模拟数据初始化完成，共创建了" + CATEGORY_NEWS_MAP.size() + "个分类");
    }
    
    /**
     * 为指定分类创建模拟新闻数据
     * @param category 分类代码
     * @param categoryName 分类名称
     */
    private static void createMockNewsForCategory(String category, String categoryName) {
        List<News> newsList = new ArrayList<>();
        
        for (int i = 1; i <= 20; i++) {
            String uniquekey = UUID.randomUUID().toString();
            String title = categoryName + "新闻" + i;
            String date = "2023-06-" + (i < 10 ? "0" + i : i);
            String authorName = "测试作者" + i;
            String url = "https://example.com/news/" + uniquekey;
            String thumbnail = "https://via.placeholder.com/400x300?text=" + categoryName + i;
            String content = generateMockContent(title, i);
            
            News news = new News(
                    uniquekey,
                    title,
                    date,
                    category,
                    authorName,
                    url,
                    thumbnail,
                    null // 内容在新闻详情中
            );
            
            news.setSource("模拟数据");
            newsList.add(news);
            
            // 创建对应的新闻详情
            createMockNewsDetail(uniquekey, title, date, authorName, content);
        }
        
        CATEGORY_NEWS_MAP.put(category, newsList);
        Log.d(TAG, "为分类 " + category + " 创建了 " + newsList.size() + " 条模拟新闻");
    }
    
    /**
     * 创建模拟新闻详情
     */
    private static void createMockNewsDetail(String uniquekey, String title, String date, String authorName, String content) {
        NewsDetailResult detailResult = new NewsDetailResult();
        detailResult.setUniquekey(uniquekey);
        detailResult.setContent(content);
        
        NewsDetailInfo detailInfo = new NewsDetailInfo();
        detailInfo.setTitle(title);
        detailInfo.setDate(date);
        detailInfo.setAuthorName(authorName);
        detailInfo.setContent(content);
        
        detailResult.setDetail(detailInfo);
        NEWS_DETAILS_MAP.put(uniquekey, detailResult);
    }
    
    /**
     * 生成模拟内容
     */
    private static String generateMockContent(String title, int index) {
        StringBuilder content = new StringBuilder();
        content.append("<h1>").append(title).append("</h1>");
        content.append("<p>这是一条模拟新闻，用于测试应用功能。</p>");
        content.append("<p>这是第").append(index).append("条新闻的详细内容。</p>");
        content.append("<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>");
        content.append("<p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>");
        content.append("<img src=\"https://via.placeholder.com/800x400?text=News+Image+").append(index).append("\" alt=\"新闻图片\">");
        content.append("<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>");
        content.append("<p>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>");
        return content.toString();
    }
    
    /**
     * 获取指定分类的新闻列表
     * @param category 分类
     * @return 新闻响应
     */
    public static NewsResponse getNewsList(String category) {
        Log.d(TAG, "获取分类 " + category + " 的模拟新闻");
        NewsResponse response = new NewsResponse();
        response.setErrorCode(0);
        response.setReason("成功的请求");
        
        NewsResponse.Result result = new NewsResponse.Result();
        result.setStat("1");
        
        List<News> newsList = CATEGORY_NEWS_MAP.get(category);
        if (newsList == null) {
            Log.w(TAG, "没有找到分类 " + category + " 的模拟数据，返回空列表");
            newsList = new ArrayList<>();
        } else {
            Log.d(TAG, "找到分类 " + category + " 的模拟数据，共 " + newsList.size() + " 条");
        }
        
        result.setData(newsList);
        response.setResult(result);
        
        return response;
    }
    
    /**
     * 获取指定ID的新闻详情
     * @param uniquekey 新闻ID
     * @return 新闻详情响应
     */
    public static NewsDetailResponse getNewsDetail(String uniquekey) {
        Log.d(TAG, "获取ID为 " + uniquekey + " 的模拟新闻详情");
        NewsDetailResponse response = new NewsDetailResponse();
        response.setErrorCode(0);
        response.setReason("成功的请求");
        
        NewsDetailResult result = NEWS_DETAILS_MAP.get(uniquekey);
        if (result == null) {
            // 如果没有找到对应的详情，创建一个默认的
            Log.w(TAG, "没有找到ID为 " + uniquekey + " 的模拟新闻详情，创建默认详情");
            result = createDefaultNewsDetail(uniquekey);
        } else {
            Log.d(TAG, "找到ID为 " + uniquekey + " 的模拟新闻详情");
        }
        
        response.setResult(result);
        return response;
    }
    
    /**
     * 创建默认的新闻详情
     */
    private static NewsDetailResult createDefaultNewsDetail(String uniquekey) {
        NewsDetailResult result = new NewsDetailResult();
        result.setUniquekey(uniquekey);
        
        String content = "<h1>找不到新闻详情</h1><p>无法找到ID为 " + uniquekey + " 的新闻详情。</p>";
        result.setContent(content);
        
        NewsDetailInfo detailInfo = new NewsDetailInfo();
        detailInfo.setTitle("找不到新闻详情");
        detailInfo.setDate("2023-06-01");
        detailInfo.setAuthorName("系统");
        detailInfo.setContent(content);
        
        result.setDetail(detailInfo);
        return result;
    }
} 